package StringSeries;

public class RepeatedSubstringPatternSolution_459_2 {
    public boolean repeatedSubstringPattern(String s) {
        int[] next = getNext(s);
        if (s.length() == 0) {
            return false;
        }

        int len = s.length();
        if (next[len - 1] != 0 && len % (len - (next[len - 1])) == 0) {
            return true;
        }
        return false;
    }

    public int[] getNext(String s) {
        int[] next = new int[s.length()];
        int j = 0;
        next[0] = 0;
        for (int i = 1; i < s.length(); i++) {
            while (j > 0 && s.charAt(i) != s.charAt(j)) {
                j = next[j - 1];
            }

            if (s.charAt(i) == s.charAt(j)) {
                j++;
            }

            next[i] = j;
        }

        return next;
    }
}
